package com.irdstudio.apicenter.mobile.console.service.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

public class AuthenticationFailureHandler extends
		SimpleUrlAuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {
		Authentication authentication = exception.getAuthentication();
		if (authentication != null) {
			WebAuthenticationDetails details = (WebAuthenticationDetails) authentication
					.getDetails();
			request.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION",
					exception);
			request.getRequestDispatcher("/").forward(request, response);
		}
	}

}
